package com.windchime.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.ResourceType;
import com.windchime.blog.entity.vo.ResourceTypeSearch;
import com.windchime.blog.mapper.ResourceTypeMapper;
import com.windchime.blog.service.ResourceTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 资源分类 服务实现类
 * </p>
 *
 * @author WindChime
 * @since 2021-08-07
 */
@Service
public class ResourceTypeServiceImpl extends ServiceImpl<ResourceTypeMapper, ResourceType> implements ResourceTypeService {

    /**
     * 分页查询--带条件
     * @param page               当前页
     * @param limit              当前页条目数
     * @param resourceTypeSearch 条件对象
     * @return resourceTypes
     */
    @Override
    public PageInfo<ResourceType> selectResourceTypeList(Integer page, Integer limit, ResourceTypeSearch resourceTypeSearch) {
        PageHelper.startPage(page, limit);
        QueryWrapper<ResourceType> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");
        if (!StringUtils.isEmpty(resourceTypeSearch.getName())) {
            wrapper.eq("name", resourceTypeSearch.getName());
        }
        if (!StringUtils.isEmpty(resourceTypeSearch.getFlag())) {
            wrapper.eq("flag", resourceTypeSearch.getFlag());
        }
        List<ResourceType> resourceTypes = baseMapper.selectList(wrapper);

        //设置父分类名称
        for (ResourceType resourceType : resourceTypes) {
            ResourceType r = baseMapper.selectById(resourceType.getParentId());
            if (r != null) {
                resourceType.setParentName(r.getName());
            } else {
                resourceType.setParentName("无");
            }
        }

        return new PageInfo<>(resourceTypes);
    }

}
